package com.hzdl.flower.controller;

import com.github.pagehelper.PageInfo;
import com.hzdl.flower.bean.Admin;
import com.hzdl.flower.bean.Flower;
import com.hzdl.flower.bean.User;
import com.hzdl.flower.service.AflowerService;
import com.hzdl.flower.service.AuserService;
import com.hzdl.flower.util.HZDLJsonResult;
import com.hzdl.flower.vo.AdminVo;
import com.hzdl.flower.vo.FlowerVo;
import com.hzdl.flower.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("aflower")
public class AflowerController {
    @Autowired
    private AflowerService afs;

    @RequestMapping("aflowerInit")
    public String aflowerInit(){
        return "admin/aflower";
    }

    @ResponseBody
    @RequestMapping("findFlower")
    public HZDLJsonResult findFlower(FlowerVo flowerVo){
        PageInfo p = afs.findFlower(flowerVo);
        return new HZDLJsonResult(0,"",p.getTotal(),p.getList());
    }

    @RequestMapping("delete")
    @ResponseBody
    public HZDLJsonResult delete(Integer id){
        int row = afs.deleteFlower(id);
        if (row == 0){
            return HZDLJsonResult.DELETE_ERROR;
        }else {
            return HZDLJsonResult.DELETE_SUCCESS;
        }
    }

    @ResponseBody
    @RequestMapping("delAll")
    public HZDLJsonResult deAll(String ids){
        String[] arr = ids.split(",");
        Integer result=0;
        for (int a=0;a<arr.length;a++){
            result += afs.deleteFlower(Integer.valueOf(arr[a]));
        }
        if (result == 0){
            return HZDLJsonResult.DELETE_ERROR;
        }else {
            return  HZDLJsonResult.DELETE_SUCCESS;
        }
    }

    @RequestMapping("checkExitChildren")
    @ResponseBody
    public HZDLJsonResult checkExitChildren(String id){
        Flower id1 = afs.findById(id);
        return new HZDLJsonResult(0,"",id1);
    }



    @RequestMapping("add")
    @ResponseBody
    public HZDLJsonResult add(FlowerVo flowerVo ){

        //区别修改还是添加，使用id1来判断
        if (flowerVo.getId1()== null ){
            int i = afs.addFlower(flowerVo);
            if (i == 0){
                return HZDLJsonResult.ADD_ERROR;
            }else {
                return HZDLJsonResult.ADD_SUCCESS;
            }

        }else {
            int i = afs.updateFlower(flowerVo);
            if (i == 0) {
                return HZDLJsonResult.UPDATE_ERROR;
            }else {
                return HZDLJsonResult.UPDATE_SUCCESS;
            }
        }

    }

    @RequestMapping("uploadImg")
    @ResponseBody
    public HZDLJsonResult uploadImg(@RequestParam("f1") MultipartFile file ) {
        try {
            // 在IE11/Edge浏览器下面，获取到的路径信息带有盘符
            //文件名称
            String filename = file.getOriginalFilename();
            // 文件上传时，Chrome和IE/Edge对于originalFilename处理不同
            // Chrome 会获取到该文件的直接文件名称，IE/Edge会获取到文件上传时完整路径/文件名
            // Check for Unix-style path
            int unixSep = filename.lastIndexOf('/');
            // Check for Windows-style path
            int winSep = filename.lastIndexOf('\\');
            // Cut off at latest possible point
            int pos = (winSep > unixSep ? winSep : unixSep);
            if (pos != -1) {
                // Any sort of path separator found...
                filename = filename.substring(pos + 1);
            }

            String path = ResourceUtils.getURL("classpath:").getPath()+"static/photo/"+filename;

            file.transferTo(new File(path));

            Map<String, Object> map = new HashMap<>();
            map.put("src", "/flower/static/photo/" + filename);

            return HZDLJsonResult.ok(map);
        } catch (Exception e) {
            return HZDLJsonResult.error(2, e.getMessage());
        }
    }







}
